package cn.ccew.sys.web.manage;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.ccew.sys.model.SysPersonnel;
import cn.ccew.sys.model.SysResources;
import cn.ccew.sys.web.SysBaseController;
import cn.ccew.sys.web.login.LoginController;

@Controller
public class MainController extends SysBaseController {

	@RequestMapping("main.wb")
	public void Index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");

		SysPersonnel admin = null;

		admin = (SysPersonnel) request.getSession().getAttribute(LoginController.curr_user_key);

		if (null != admin && StringUtils.isNotEmpty(admin.getLoginName())) {
			response.getWriter().print(getMainHTML(admin.getLoginName(), request));
		} else {
			// response.getWriter().print("<script type=\"text/javascript\" language=\"javascript\">alert('您会话超时,重新登录!'); location.href = \"login.wb\";</script>");
			response.getWriter().print("<script type=\"text/javascript\" language=\"javascript\"> location.href = \"login.wb\";</script>");
		}
	}

	private String getMainHTML(String userName, HttpServletRequest request) throws Exception {
		StringBuilder sbhtmlfoot = new StringBuilder();
		StringBuilder sbhtml = new StringBuilder();
		StringBuilder sbjs = new StringBuilder();

		sbjs.append("<script type=\"text/javascript\" src=\"../manage/js/" + "member.js" + "\"></script>");
		sbjs.append("<script type=\"text/javascript\" src=\"../manage/js/" + "depmanage.js" + "\"></script>");
		sbhtml.append("<link rel=\"shortcut icon\" href=\"../manage/kindeditor/themes/default/default.css\"  />");
		sbjs.append("<script type=\"text/javascript\" src=\"../manage/kindeditor/kindeditor-all-min.js" + "\"></script>");
		sbjs.append("<script type=\"text/javascript\" src=\"../manage/kindeditor/lang/zh_CN.js" + "\"></script>");
		sbjs.append("<script type=\"text/javascript\" src=\"../manage/kindeditor/plugins/code/prettify.js" + "\"></script>");

		sbjs.append("<script type=\"text/javascript\" src=\"../manage/Highcharts-3.0.7/js/highcharts.js" + "\"></script>");
		sbjs.append("<script type=\"text/javascript\" src=\"../manage/Highcharts-3.0.7/js/modules/exporting.js" + "\"></script>");

		// sbjs.append("<script type=\"text/javascript\" src=\"../manage/js/" +
		// "city.js" + "\"></script>");

		// sbjs.append("<script type=\"text/javascript\" src=\"../manage/js/" +
		// "sys/sys_resource.js" + "\"></script>");

		sbhtml.append("<html><head><title> android版本管理后台</title><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");

		sbhtml.append("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE8\" />");
		sbhtml.append("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
		sbhtml.append("<link rel=\"shortcut icon\" href=\"../manage/images/weblogn.png\"  />");
		sbhtml.append("<style type=\"text/css\">");
		sbhtml.append("#loading{position:absolute;left:40%;top:40%;padding:2px;z-index:20001;height:auto;}#loading a {color:#225588;}");
		sbhtml.append("#loading .loading-indicator{background:white;color:#444;font:bold 13px tahoma,arial,helvetica;padding:10px;margin:0;height:auto;}");
		sbhtml.append("#loading-msg {font: normal 10px arial,tahoma,sans-serif;}");
		sbhtml.append("</style></head>");
		sbhtml.append("<body>");

		sbhtml.append("<div id=\"loading\"><div class=\"loading-indicator\">");
		sbhtml.append("<img src=\"../manage/images/loading32.gif\" width=\"31\" height=\"31\" style=\"margin-right:8px;float:left;vertical-align:top;\" />");
		sbhtml.append(" android版本管理后台");
		sbhtml.append("<br/><span id=\"loading-msg\">加载样式和图片...</span></div></div>");

		sbhtml.append("<div id=\"bd\">");
		sbhtml.append("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"../manage/extjs/resources/css/ext-all.css\" />");
		sbhtml.append("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"../manage/css/index.css\" />");
		sbhtml.append("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"../manage/css/data_view.css\" />");
		sbhtml.append("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"../manage/extjs/ux/fileuploadfield/css/fileuploadfield.css\" />");
		sbhtml.append("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"../manage/css/firefox_ext.css\" />");
		sbhtml.append("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"../manage/css/Spinner.css\" />");
		sbhtml.append("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"../manage/extjs/shared/examples.css\" />");

		sbhtml.append("<script type=\"text/javascript\">document.getElementById('loading-msg').innerHTML = '加载核心...';</script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/adapter/ext/ext-base.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\">document.getElementById('loading-msg').innerHTML = '加载UI组件...';</script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/ext-all.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/ux/TabCloseMenu.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/ux/SpinnerField.js\"></script>");

		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/shared/examples.js\"></script>");

		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/Spinner.js\"></script>");

		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/js/CJ_StarHtmlEditor.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/js/f13HtmlEditor.js\"></script>");

		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/SpinnerField.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/DateTimeField.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\">document.getElementById('loading-msg').innerHTML = '模块初始化...';</script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/js/main.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/ux/fileuploadfield/FileUploadField.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/ux/DataView-more.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/js/md5.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/js/functionTree.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/js/Component.js\"></script>");
		//修改加的js---开始
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/ux/RowEditor.js\"></script>");
		sbhtml.append("<script type=\"text/javascript\" src=\"../manage/extjs/source/widgets/grid/GroupingView.js\"></script>");
		//修改加的js---结束
		sbhtmlfoot.append("</div><div id=\"header\">");

		sbhtmlfoot.append("<table style=\"width: 100%;\"><tr><td><h1><font color=\"white\" >  版本管理后台---当前用户:</font><font >" + userName + "</font></h1></td>");
		sbhtmlfoot.append("<td style=\"text-align: right\" align=\"right\" >");

		// sbhtmlfoot.append("<h1><a href=\"javascript:void(0)\" onclick=\"changePassword()\" ><font color=\"white\" >修改密码</font></a>");
		// sbhtmlfoot.append(" &nbsp; &nbsp;<a href=\"javascript:void(0)\" onclick=\"loginout()\" ><font color=\"white\" >退出系统</font></a></h1>");

		sbhtmlfoot.append("<div id='mainColumnPanel' style=\"text-align:right\"/>");
		sbhtmlfoot.append("<script language=\"javascript\">");
		sbhtmlfoot.append("var mainColumnPanelInfo=[");

		List<SysResources> list = null;
		// 加载系统模块
		try {
			Set<SysResources> rsets = this.getCurrAdmin(request).getRole().getResources();

			Criteria criteria = baseService.createCriteria(SysResources.class);
			criteria.add(Restrictions.isNull("parent"));
			criteria.addOrder(Order.asc("priority"));
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			list = criteria.list();

			for (SysResources temp : list) {
				if (rsets.contains(temp)) {
					sbhtmlfoot.append("{title: '" + temp.getRsName() + "',code:'" + temp.getId() + "'},");
				}
			}
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		sbhtmlfoot.append("{title: '修改密码',code:'-1'},");
		sbhtmlfoot.append("{ title: '退出系统',code:'0'}");
		sbhtmlfoot.append("];");
		String cree = "";
		if (null != list && list.size() > 0) {
			cree = list.get(0).getId();
		}
		sbhtmlfoot.append("var crurrCode='" + cree + "';");
		sbhtmlfoot.append("</script>");

		sbhtmlfoot.append("</td>");
		sbhtmlfoot.append("</tr></div></body></html>");

		return sbhtml.toString() + sbjs.toString() + sbhtmlfoot.toString();

	}

}
